﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DataAccess_Layer
{
    public class NCC_GiangHang_DAC
    {
        public static object layDanhSach()
        {
            return DataAccessHelper.DB.NCC_GiangHangs.Select(ncg => new { ncg.ID, ncg.Hinh, ncg.NCC_UN, ncg.Ten, TenCLSP = ncg.ChungLoaiSP.Ten, ncg.MoTa, ncg.Gia, ncg.SLToiThieu, ncg.NgayCapNhatSaucung, ncg.MaChungLoaiSP });
        }
       
        public static object timKiem_NCCGH(string tenDN,string tenNCC, long? giaTu, long? giaDen)
        {
            //4 cai null
            if (tenDN != null && tenNCC != null && giaTu != null && giaDen != null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.NCC_UN.Contains(tenDN) && pp.Ten.Contains(tenNCC) && pp.Gia >= giaTu && pp.Gia <= giaDen).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }

            //1 cai null
            if (tenDN == null && tenNCC != null && giaTu != null && giaDen != null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.Ten.Contains(tenNCC) && pp.Gia >= giaTu && pp.Gia <= giaDen).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN != null && tenNCC == null && giaTu != null && giaDen != null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.NCC_UN.Contains(tenDN) && pp.Gia >= giaTu && pp.Gia <= giaDen).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN != null && tenNCC != null && giaTu == null && giaDen != null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.NCC_UN.Contains(tenDN) && pp.Ten.Contains(tenNCC) && pp.Gia <= giaDen).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN != null && tenNCC != null && giaTu != null && giaDen == null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.NCC_UN.Contains(tenDN) && pp.Ten.Contains(tenNCC) && pp.Gia >= giaTu ).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }

            //2 cai null
            if (tenDN == null && tenNCC == null && giaTu != null && giaDen != null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.Gia >= giaTu && pp.Gia <= giaDen).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN == null && tenNCC != null && giaTu == null && giaDen != null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.Ten.Contains(tenNCC) && pp.Gia <= giaDen).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN == null && tenNCC != null && giaTu != null && giaDen == null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.Ten.Contains(tenNCC) && pp.Gia >= giaTu).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
           
            if (tenDN != null && tenNCC == null && giaTu == null && giaDen != null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.NCC_UN.Contains(tenDN)  && pp.Gia <= giaDen).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN != null && tenNCC == null && giaTu != null && giaDen == null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.NCC_UN.Contains(tenDN)  && pp.Gia >= giaTu ).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN != null && tenNCC != null && giaTu == null && giaDen == null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.NCC_UN.Contains(tenDN) && pp.Ten.Contains(tenNCC) ).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }

            //3 cai null
            if (tenDN == null && tenNCC == null && giaTu == null && giaDen != null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.Gia <= giaDen).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN != null && tenNCC == null && giaTu == null && giaDen == null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.NCC_UN.Contains(tenDN) ).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN == null && tenNCC != null && giaTu == null && giaDen == null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp =>pp.Ten.Contains(tenNCC) ).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }
            if (tenDN == null && tenNCC == null && giaTu != null && giaDen == null)
            {
                return DataAccessHelper.DB.NCC_GiangHangs.Where(pp => pp.Gia >= giaTu).Select(pp => new { pp.ID, pp.NCC_UN, pp.Ten, TenCLSP = pp.ChungLoaiSP.Ten, pp.MoTa, pp.Hinh, pp.Gia, pp.SLToiThieu, pp.NgayCapNhatSaucung, pp.MaChungLoaiSP });
            }

            return layDanhSach();
        }

        public static void delete(long id)
        {
            NCC_GiangHang nccgh = DataAccessHelper.DB.NCC_GiangHangs.Where(ncc=>ncc.ID == id).Select(ncc=>ncc).First();
            DataAccessHelper.DB.NCC_GiangHangs.DeleteOnSubmit(nccgh);
            DataAccessHelper.Save();
        }

        public static void delete(List<long> deletedIDs)
        {
            foreach (long deletedID in deletedIDs)
            {
                NCC_GiangHang nccgh = DataAccessHelper.DB.NCC_GiangHangs.Where(ncc => ncc.ID == deletedID).Select(ncc => ncc).First();
                DataAccessHelper.DB.NCC_GiangHangs.DeleteOnSubmit(nccgh);
            }

            DataAccessHelper.Save();
        }

        public static void update(NCC_GiangHang nCC_GiangHang)
        {
            NCC_GiangHang nccgh = DataAccessHelper.DB.NCC_GiangHangs.Where(ncc => ncc.ID == nCC_GiangHang.ID).Select(ncc => ncc).First();
            nccgh.MaChungLoaiSP = nCC_GiangHang.MaChungLoaiSP;
            nccgh.MoTa = nCC_GiangHang.MoTa;
            nccgh.NCC_UN = nCC_GiangHang.NCC_UN;
            nccgh.NgayCapNhatSaucung = nCC_GiangHang.NgayCapNhatSaucung;
            nccgh.SLToiThieu = nCC_GiangHang.SLToiThieu;
            nccgh.Ten = nCC_GiangHang.Ten;
            nccgh.Gia = nCC_GiangHang.Gia;
            nccgh.Hinh = nCC_GiangHang.Hinh;

            DataAccessHelper.Save();
        }
    }
}
